package cc.rengu.igas.bomp.core.realize.impl;

import cc.rengu.igas.bomp.common.constant.DubboMethodConstant;
import cc.rengu.igas.bomp.common.constant.TableNameConstant;
import cc.rengu.igas.bomp.common.dao.BizTmpMapper;
import cc.rengu.igas.bomp.common.dao.MchntImageInfoMapper;
import cc.rengu.igas.bomp.common.dao.WechatMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.BizTmpMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntImageInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.WechatMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.BizTmp;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.entity.MchntImageInfo;
import cc.rengu.igas.bomp.common.entity.WechatMchntInfo;
import cc.rengu.igas.bomp.common.enums.BompBizOprEnum;
import cc.rengu.igas.bomp.common.enums.MchntStatusEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.common.enums.SysParamEnum;
import cc.rengu.igas.bomp.common.util.InitParamUtil;
import cc.rengu.igas.bomp.core.realize.MchntImageUnifiedService;
import cc.rengu.igas.bomp.facade.bean.ImageInfoBean;
import cc.rengu.igas.bomp.facade.bean.MchntImageInfoBean;
import cc.rengu.igas.channel.wechat.facade.base.Header;
import cc.rengu.igas.channel.wechat.facade.request.WechatUploadImageRequest;
import cc.rengu.igas.channel.wechat.facade.response.WechatUploadImageResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author bank
 * @version 1.0
 * @date 2019/08/15
 */
public class MchntImageUnifiedServiceImpl implements MchntImageUnifiedService {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    private int uploadToWechatCallIndex = 0;

    private String imageBasePath;

    //private String testImagePath = "E:\\testimgae";

    public MchntImageUnifiedServiceImpl() {
    }

    public MchntImageUnifiedServiceImpl(String imageBasePath) {
        this.imageBasePath = imageBasePath;
    }

    /**
     * 影像管理通用方法
     * <p/>
     * 调用前需开启MCMP数据库事务,并且以try-catch包裹,同时建议提前判断mchntImageInfoBeanList是否为空,非空时传入
     * 例子:
     * {@link cc.rengu.igas.bomp.core.service.trans.MchntImageInfoManageService}
     *
     * @param mchntBaseInfo          商户基本信息
     * @param mchntImageInfoBeanList 上送影像List
     * @param imageTypes             [,]分割的预处理影像类型
     * @throws Exception 异常
     */
    @Override
    public void manageMchntImage(MchntBaseInfo mchntBaseInfo, List<MchntImageInfoBean> mchntImageInfoBeanList, String imageTypes) throws Exception {
        //idMap,初始将临时表中有效的数据存在的放入map,key存放影像的ID,value为0表示临时表中存在的;为1表示上送信息中存在的ID
        Map<String, String> idMap = new HashMap<>();

        //查询临时表
        BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
        List<BizTmp> dbMchntImageInfoBizTmpList = bizTmpMapper.selectBizTmpByMchntNo(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo(), TableNameConstant.T_B_MCHNT_IMAGE_INFO);
        List<MchntImageInfo> bizTmpMchntImageInfoList = new ArrayList<>();
        if (CollectionUtils.isEmpty(dbMchntImageInfoBizTmpList)) {
            dbMchntImageInfoBizTmpList = new ArrayList<>();
        }
        for (BizTmp bizTmp : dbMchntImageInfoBizTmpList) {
            MchntImageInfo mchntImageInfo = JSONObject.parseObject(InitParamUtil.getJsonString(bizTmp), MchntImageInfo.class);
            if ("Y".equals(mchntImageInfo.getRecordStatus()) && imageTypes.contains(mchntImageInfo.getImageType())) {
                idMap.put(bizTmp.getRealId(), "0");
                bizTmpMchntImageInfoList.add(mchntImageInfo);
            }
        }

        //转换数据库影像信息,非正式商户并更新至正式表
        List<MchntImageInfo> mchntImageInfoList = mchntImageInfoBeanListChangeToMchntImageInfoList(mchntBaseInfo, mchntImageInfoBeanList, dbMchntImageInfoBizTmpList, idMap);
        //转换临时表
        List<BizTmp> bizTmpList = initBizTmpMchntImageInfo(mchntBaseInfo, mchntImageInfoList);
        // imagePlatFormUpProcess(mchntBaseInfo, bizTmpMchntImageInfoList, bizTmpList);

        rglog.debug("数据转换临时表成功.");
        //刷新临时表
        InitParamUtil.refreshBizTmp(mchntBaseInfo, bizTmpList, idMap, rglog);
    }

    /**
     * 将上送图片转换为一个列表处理
     */
    private List<MchntImageInfo> mchntImageInfoBeanListChangeToMchntImageInfoList(MchntBaseInfo mchntBaseInfo, List<MchntImageInfoBean> mchntImageInfoBeanList, List<BizTmp> dbMchntImageInfoBizTmpList, Map<String, String> idMap) throws Exception {
        List<MchntImageInfo> mchntImageInfoList = new ArrayList<>();

        //查询库中数据
        MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
        List<MchntImageInfo> dbMchntImageInfoList = mchntImageInfoMapper.selectMchntImageInfoByMchntNo(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo());
        if (null == dbMchntImageInfoList) {
            //预防空指针
            dbMchntImageInfoList = new ArrayList<>();
        }

        for (MchntImageInfoBean mchntImageInfoBean : mchntImageInfoBeanList) {
            List<ImageInfoBean> imageInfoBeanList = mchntImageInfoBean.getImageInfoList();
            if (CollectionUtils.isEmpty(imageInfoBeanList)) {
                rglog.error("管理商户影像信息失败, 影像信息参数为空！imageType:{}", mchntImageInfoBean.getImageType());
                // throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
                continue;
            }
            for (ImageInfoBean imageInfoBean : imageInfoBeanList) {
                /**开始处理单张图片*/
                MchntImageInfo mchntImageInfo = mchntImageInfoBeanChangeToMchntImageInfo(mchntBaseInfo, dbMchntImageInfoList, dbMchntImageInfoBizTmpList, mchntImageInfoBean.getImageType(), imageInfoBean);
                mchntImageInfoList.add(mchntImageInfo);
                idMap.put(mchntImageInfo.getId(), "1");
            }
        }
        return mchntImageInfoList;
    }

    /**
     * 上送影像信息转数据库影像信息
     *
     * @param mchntBaseInfo              商户基本信息
     * @param dbMchntImageInfoList       数据库影像信息列表
     * @param dbMchntImageInfoBizTmpList 数据库影像信息临时表
     * @param imageType                  影像类型
     * @param imageInfoBean              单个影像信息
     * @return 数据库影像信息
     * @throws Exception 异常
     */
    private MchntImageInfo mchntImageInfoBeanChangeToMchntImageInfo(MchntBaseInfo mchntBaseInfo, List<MchntImageInfo> dbMchntImageInfoList, List<BizTmp> dbMchntImageInfoBizTmpList, String imageType, ImageInfoBean imageInfoBean) throws Exception {

        //图片ID
        String id;
        //是否为新增图片
        boolean isNewImage;
        if (StringUtil.isEmptyOrNull(imageInfoBean.getImageLocalPath())) {
            rglog.error("上送影像信息ID<{}>,imageType<{}>,imageIndex<{}>,未上送图片本地存储路径.", imageInfoBean.getId(), imageType, imageInfoBean.getImageIndex());
            throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
        }
        rglog.debug("ID<{}>,imageType<{}>,index<{}>,imageLocalPath<{}>",
                imageInfoBean.getId(), imageType, imageInfoBean.getImageIndex(), new String(Base64.decodeBase64(imageInfoBean.getImageLocalPath())));

        /**影像信息*/
        MchntImageInfo mchntImageInfo = new MchntImageInfo();
        BeanUtil.beanCopy(mchntBaseInfo, mchntImageInfo);

        List<MchntImageInfo> filterMchntImageInfoList = dbMchntImageInfoList.stream().filter(item -> item.getImageType().equals(imageType) && item.getImageIndex() == Integer.parseInt(imageInfoBean.getImageIndex())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(filterMchntImageInfoList)) {
            //正式表没有imageType和imageIndex相同的数据,新增图片,获取新ID
            isNewImage = true;
            id = RandomUtil.getUUID();
            rglog.debug("新增图片ID<{}>", id);
            mchntImageInfo.setId(id);
            mchntImageInfo.setOprId(mchntBaseInfo.getLastOprId());
            mchntImageInfo.setCreateTime(mchntBaseInfo.getLastUpdateTime());
        } else {
            //已有ID,存在数据
            isNewImage = false;
            id = filterMchntImageInfoList.get(0).getId();
            //原商户影像信息
            MchntImageInfo origMchntImageInfo;
            List<BizTmp> bizTmpList = dbMchntImageInfoBizTmpList.stream().filter(item -> item.getRealId().equals(id)).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(bizTmpList)) {
                List<MchntImageInfo> imageInfoList = dbMchntImageInfoList.stream().filter(item -> item.getId().equals(imageInfoBean.getId())).collect(Collectors.toList());
                if (CollectionUtils.isEmpty(imageInfoList)) {
                    rglog.error("正式表无ID为<{}>的正式表数据", id);
                    throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
                }
                origMchntImageInfo = imageInfoList.get(0);
            } else {
                origMchntImageInfo = JSONObject.parseObject(InitParamUtil.getJsonString(bizTmpList.get(0)), MchntImageInfo.class);
            }

            BeanUtil.beanCopy(origMchntImageInfo, mchntImageInfo);
        }

        BeanUtil.beanCopy(imageInfoBean, mchntImageInfo);
        //ID赋值
        mchntImageInfo.setId(id);
        if (StringUtil.isEmptyOrNull(imageInfoBean.getRecordStatus())) {
            //默认有效
            mchntImageInfo.setRecordStatus(AppParamConstant.YES);
        } else {
            mchntImageInfo.setRecordStatus(imageInfoBean.getRecordStatus());
        }

        mchntImageInfo.setImageType(imageType);
        mchntImageInfo.setImageLocalPath(new String(Base64.decodeBase64(imageInfoBean.getImageLocalPath())));
        if (!StringUtil.isEmptyOrNull(imageInfoBean.getImagePath())) {
            mchntImageInfo.setImageRemotePath(new String(Base64.decodeBase64(imageInfoBean.getImagePath())));
        }
        mchntImageInfo.setWechatMediaId(imageInfoBean.getWechatImageId());
        mchntImageInfo.setImageDesc(imageInfoBean.getImageDesc());

        MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
        if (MchntStatusEnum.NORMAL.getStatus().equals(mchntBaseInfo.getMchntStatus())) {
            if (isNewImage) {
                //新增图片还需要登记至正式表,
                MchntImageInfo dbMchntImageInfo = new MchntImageInfo();
                BeanUtil.beanCopy(mchntImageInfo, dbMchntImageInfo);
                dbMchntImageInfo.setRecordStatus(AppParamConstant.NO);
                int insertResult = mchntImageInfoMapper.insertMchntImageInfo(dbMchntImageInfo);
                if (0 != insertResult) {
                    rglog.error("新增数据<{}>到正式表失败.", dbMchntImageInfo.getId());
                    throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
                }
            }
        } else {
            // if (MchntStatusEnum.UNNORMAL.getStatus().equals(mchntBaseInfo.getMchntStatus())) {
            //非正式商户(新增流程中),还需要将影像信息新增/更新至正式表,并且记录状态为有效
            MchntImageInfo dbMchntImageInfo = new MchntImageInfo();
            BeanUtil.beanCopy(mchntImageInfo, dbMchntImageInfo);
            if (isNewImage) {
                int insertResult = mchntImageInfoMapper.insertMchntImageInfo(dbMchntImageInfo);
                if (0 != insertResult) {
                    rglog.error("新增数据<{}>到正式表失败.", dbMchntImageInfo.getId());
                    throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
                }
            } else {
                int updateResult = mchntImageInfoMapper.updateMchntImageInfoById(dbMchntImageInfo);
                if (0 != updateResult) {
                    rglog.error("更新数据<{}>到正式表失败.", dbMchntImageInfo.getId());
                    throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
                }
            }
        }

        return mchntImageInfo;
    }

    /**
     * 将上送影像信息转换为临时表影像信息
     *
     * @param mchntBaseInfo      商户基本信息
     * @param mchntImageInfoList 上送图片列表
     * @return 操作完成后的临时表信息
     * @throws Exception 异常
     */
    private List<BizTmp> initBizTmpMchntImageInfo(MchntBaseInfo mchntBaseInfo, List<MchntImageInfo> mchntImageInfoList) throws Exception {
        List<BizTmp> bizTmpList = new ArrayList<>();
        for (MchntImageInfo mchntImageInfo : mchntImageInfoList) {
            rglog.debug("准备将ID<{}>,imageType<{}>,index<{}>,imageLocalPath<{}>转为临时表数据.",
                    mchntImageInfo.getId(), mchntImageInfo.getImageType(), mchntImageInfo.getImageIndex(), mchntImageInfo.getImageLocalPath());
            String bizOpr = BompBizOprEnum.UPDATE.getBizOprCode();
            if (mchntBaseInfo.getLastUpdateTime().equals(mchntImageInfo.getCreateTime())) {
                bizOpr = BompBizOprEnum.INSERT.getBizOprCode();
            }
            BizTmp bizTmp = InitParamUtil.initBizTmpInfo(mchntBaseInfo, TableNameConstant.T_B_MCHNT_IMAGE_INFO, bizOpr, mchntImageInfo);
            bizTmpList.add(bizTmp);
        }
        return bizTmpList;
    }

    @Override
    public void uploadToWechat(MchntBaseInfo mchntBaseInfo, WechatMchntInfo wechatMchntInfo, MchntImageInfo mchntImageInfo) throws Exception {
        if (null == mchntImageInfo) {
            rglog.error("影像为空.");
            throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
        }
        if (StringUtil.isEmptyOrNull(imageBasePath)) {
            SysParam imagePathSysParam = SysParamEnum.getSysParam(mchntBaseInfo.getInstId(), SysParamEnum.localStorageRootPath);
            if (null != imagePathSysParam && AppParamConstant.YES.equals(imagePathSysParam.getParamStatus())) {
                imageBasePath = imagePathSysParam.getParamValue() + File.separator;
            } else {
                throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
            }
        }
        String mediaId = uploadToWechat(mchntBaseInfo, wechatMchntInfo, imageBasePath + mchntImageInfo.getImageLocalPath());
        updateImageMediaId(mchntBaseInfo, mchntImageInfo, mediaId);
    }

    @Override
    public String uploadToWechat(MchntBaseInfo mchntBaseInfo, WechatMchntInfo wechatMchntInfo, String localPath) throws Exception {
        if (null == wechatMchntInfo) {
            WechatMchntInfoMapper wechatMchntInfoMapper = new WechatMchntInfoMapperImpl();
            wechatMchntInfo = wechatMchntInfoMapper.selectWechatMchntInfoByPrimaryKey(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo());
            if (null == wechatMchntInfo) {
                rglog.error("图片文件上传同时上传微信失败,缺少微信商户信息!");
                throw new BizException(RespCodeEnum.ILLEGAL_OPERATION.getRespCode(), RespCodeEnum.ILLEGAL_OPERATION.getRespDesc());
            }
        }

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);

        WechatUploadImageRequest uploadImageRequest = new WechatUploadImageRequest();
        String channelSeqNum = sysSeqNum + uploadToWechatCallIndex++;
        Header wechatHeader = InitParamUtil.initWechatHeader("uploadimage", mchntBaseInfo.getInstId(), channelSeqNum);
        uploadImageRequest.setHeader(wechatHeader);
        uploadImageRequest.setFilePath(localPath);
        uploadImageRequest.setChannelType(wechatMchntInfo.getDstChannelType());
        uploadImageRequest.setChannelId(ChannelEnum.WXZF.getChannelType());
        DubboService dubboService = new DubboServiceImpl();
        Result result = (Result) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_PUB_SERVICE, "uploadImage", uploadImageRequest);
        if (null == result || null == result.getResult()) {
            rglog.error("调用微信图片上传失败失败!");
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        WechatUploadImageResponse wechatUploadImageResponse = (WechatUploadImageResponse) result.getResult();
        if (!result.isSuccess() || !DubboMethodConstant.DUBBO_SUCCESS.equals(wechatUploadImageResponse.getRespCode())) {
            rglog.error("微信图片上传失败,错误码：<{}>，错误描述：<{}>", wechatUploadImageResponse.getRespCode(), wechatUploadImageResponse.getRespDesc());
            throw new BizException(wechatUploadImageResponse.getRespCode(), wechatUploadImageResponse.getRespDesc());
        }
        if (StringUtil.isEmptyOrNull(wechatUploadImageResponse.getMediaId())) {
            rglog.error("微信未返回媒体文件标识!");
            throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "微信未返回媒体文件标识");
        }
        return wechatUploadImageResponse.getMediaId();
    }

    private void updateImageMediaId(MchntBaseInfo mchntBaseInfo, MchntImageInfo mchntImageInfo, String mediaId) throws Exception {
        DbsUtil dbsUtil = InitParamUtil.dbsBeginTransaction(AppConfigConstant.MCMP_POOL_NAME);
        try {
            mchntImageInfo.setWechatMediaId(mediaId);
            MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
            int dbResult = mchntImageInfoMapper.updateMchntImageInfoById(mchntImageInfo);
            if (0 != dbResult) {
                rglog.error("更新影像信息<{}>失败.", mchntImageInfo.getId());
                throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
            }

            BizTmp bizTmp = InitParamUtil.initBizTmpInfo(mchntBaseInfo, TableNameConstant.T_B_MCHNT_IMAGE_INFO, BompBizOprEnum.UPDATE.getBizOprCode(), mchntImageInfo);
            BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
            dbResult = bizTmpMapper.updateBizTmpByRealId(bizTmp);
            if (0 != dbResult) {
                rglog.error("更新影像信息<{}>临时表失败.", mchntImageInfo.getId());
                throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
            }
            dbsUtil.dbsEndTransaction(true);
        } catch (Exception e) {
            dbsUtil.dbsEndTransaction(false);
            rglog.error("更新影像信息<{}>失败.", mchntImageInfo.getId());
            throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
        }
    }

    // /**
    //  * 创建商户影像平台目录
    //  *
    //  * @param mchntBaseInfo 商户基本信息
    //  */
    // public void createMchntImageHome(MchntBaseInfo mchntBaseInfo) throws Exception {
    //     BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
    //     List<BizTmp> mchntImageInfoBizTmpList = bizTmpMapper.selectBizTmpByMchntNo(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo(), TableNameConstant.T_B_MCHNT_IMAGE_INFO);
    //     if (CollectionUtils.isEmpty(mchntImageInfoBizTmpList)) {
    //         rglog.error("商户无影像信息临时表.");
    //         return;
    //     }
    //     MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
    //     List<MchntImageInfo> mchntImageInfoList = mchntImageInfoMapper.selectMchntImageInfoByMchntNo(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo());
    //     if (CollectionUtils.isEmpty(mchntImageInfoList)) {
    //         rglog.error("商户无影像信息正式表.");
    //         return;
    //     }
    //
    //     XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
    //     ImagePlatfromBean imagePlatfromBean = new ImagePlatfromBean();
    //     imagePlatfromBean.setFilePartName(mchntBaseInfo.getMchntNo());
    //     imagePlatfromBean.setBusiSerialNo(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
    //     List<ImagePlatformFileBean> imagePlatformFileBeanList = new ArrayList<>();
    //     imagePlatfromBean.setFileBeas(imagePlatformFileBeanList);
    //
    //     String contentId =  ImagePlatformUtil.uploadImage(imagePlatfromBean, rglog);
    //     if (StringUtil.isEmptyOrNull(contentId)) {
    //         rglog.error("影像平台创建目录错误.");
    //         throw new BizException(RespCodeEnum.IMAGE_PLAT_FROM_ERROR.getRespCode(), RespCodeEnum.IMAGE_PLAT_FROM_ERROR.getRespDesc());
    //     }
    //
    //     mchntBaseInfo.setRemark3(contentId);
    //     BizTmp mchntBaseInfoBizTmp = bizTmpMapper.selectBizTmpByRealId(mchntBaseInfo.getInstId(), mchntBaseInfo.getId());
    //     if (null == mchntBaseInfoBizTmp) {
    //         rglog.error("商户<{}>基本信息临时表为空.", mchntBaseInfo.getMchntNo());
    //         throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
    //     }
    //     JSONObject jsonObject = JSONObject.parseObject(InitParamUtil.getJsonString(mchntBaseInfoBizTmp));
    //     jsonObject.put("remark3",contentId);
    //     InitParamUtil.initBizTempData(mchntBaseInfoBizTmp, jsonObject.toJSONString());
    //
    //     DbsUtil dbsUtil = InitParamUtil.dbsBeginTransaction(AppConfigConstant.MCMP_POOL_NAME);
    //     try {
    //         MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
    //         int dbResult = mchntBaseInfoMapper.updateMchntBaseInfoById(mchntBaseInfo);
    //         if (0 != dbResult) {
    //             rglog.error("更新商户信息失败.");
    //             throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
    //         }
    //
    //         dbResult = bizTmpMapper.updateBizTmpByRealId(mchntBaseInfoBizTmp);
    //         if (0 != dbResult) {
    //             rglog.error("更新商户信息<{}>临时表失败.", mchntBaseInfo.getId());
    //             throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
    //         }
    //         dbsUtil.dbsEndTransaction(true);
    //     } catch (Exception e) {
    //         dbsUtil.dbsEndTransaction(false);
    //         rglog.error(cc.rengu.utility.base.StringUtil.ExceptionToString(e));
    //         throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
    //     }
    // }
    //
    // /**
    //  * 图片上传影像平台
    //  */
    // private void imagePlatFormUpProcess(MchntBaseInfo mchntBaseInfo, List<MchntImageInfo> bizTmpMchntImageInfoList, List<BizTmp> bizTmpList) throws Exception {
    //     SysParam sysParam = SysParamEnum.getSysParam(mchntBaseInfo.getInstId(), SysParamEnum.IMAGE_PLAT_FORM_FLAG);
    //     if (null == sysParam || AppParamConstant.NO.equals(sysParam.getParamStatus()) || AppParamConstant.NO.equals(sysParam.getParamValue())) {
    //         rglog.error("本行未配置影像平台.");
    //         return;
    //     }
    //     rglog.info("开始检查影像并将影像上传影像平台.");
    //     MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
    //
    //     String parentPath = getImageParentPath(mchntBaseInfo.getInstId());
    //     for (BizTmp bizTmp : bizTmpList) {
    //         MchntImageInfo mchntImageInfo = JSONObject.parseObject(InitParamUtil.getJsonString(bizTmp), MchntImageInfo.class);
    //         if (AppParamConstant.NO.equals(mchntImageInfo.getRecordStatus())) {
    //             rglog.debug("影像<{}>记录状态<{}>为删除影像,不进行处理.", mchntImageInfo.getId(), mchntImageInfo.getRecordStatus());
    //             continue;
    //         }
    //         List<MchntImageInfo> orgBizTmpMchntImageInfoList = bizTmpMchntImageInfoList.stream().filter(item -> mchntImageInfo.getId().equals(item.getId())).collect(Collectors.toList());
    //         if (!CollectionUtils.isEmpty(orgBizTmpMchntImageInfoList)) {
    //             MchntImageInfo orgBizTmpMchntImageInfo = orgBizTmpMchntImageInfoList.get(0);
    //             if (mchntImageInfo.getImageLocalPath().equals(orgBizTmpMchntImageInfo.getImageLocalPath())) {
    //                 rglog.info("影像<{}>地址<{}>未变更,不进行处理.", mchntImageInfo.getId(), mchntImageInfo.getImageLocalPath());
    //                 continue;
    //             }
    //         }
    //
    //         rglog.info("影像<{}>开始上传影像平台.", mchntImageInfo.getId());
    //
    //         /**上传到影像平台*/
    //         upToImagePlatForm(mchntImageInfo, parentPath);
    //         /**将影像平台返回的信息传入临时表*/
    //         InitParamUtil.initBizTempData(bizTmp, JSONObject.toJSONString(mchntImageInfo));
    //         /**非正式商户刷新正式表*/
    //         if (MchntStatusEnum.UNNORMAL.getStatus().equals(mchntBaseInfo.getMchntStatus())) {
    //             mchntImageInfo.setRecordStatus(AppParamConstant.NO);
    //             mchntImageInfoMapper.updateMchntImageInfoById(mchntImageInfo);
    //         }
    //     }
    // }
    //
    // /**
    //  * 上传影像平台
    //  */
    // private void upToImagePlatForm(MchntImageInfo mchntImageInfo, String parentPath) {
    //     int index = mchntImageInfo.getImageLocalPath().indexOf(".");
    //     String fileFormt = mchntImageInfo.getImageLocalPath().substring(index);
    //     String localPath = parentPath + mchntImageInfo.getImageLocalPath();
    //
    //     ImagePlatfromBean imagePlatfromBean = new ImagePlatfromBean();
    //     imagePlatfromBean.setFilePartName(localPath);
    //     imagePlatfromBean.setBusiSerialNo(RandomUtil.getUUID());
    //     List<ImagePlatformFileBean> imagePlatformFileBeanList = new ArrayList<>();
    //     imagePlatfromBean.setFileBeas(imagePlatformFileBeanList);
    //     ImagePlatformFileBean imagePlatformFileBean = new ImagePlatformFileBean();
    //     imagePlatformFileBeanList.add(imagePlatformFileBean);
    //     imagePlatformFileBean.setLocalFilePath(localPath);
    //     // imagePlatformFileBean.setFileName();
    //     imagePlatformFileBean.setFileFormt(fileFormt);
    //
    //     rglog.info("上传影像平台:" + JSONObject.toJSONString(imagePlatfromBean));
    //     String uploadImageResult = ImagePlatformUtil.uploadImage(imagePlatfromBean, rglog);
    //     if (null != uploadImageResult) {
    //         rglog.info("上传结果：" + uploadImageResult);
    //         mchntImageInfo.setImageRemotePath(uploadImageResult);
    //     }
    // }
    //
    // // @Override
    // public boolean imagePlatFormDownloadProcess(MchntImageInfo mchntImageInfo) throws Exception {
    //     SysParam sysParam = SysParamEnum.getSysParam(mchntImageInfo.getInstId(), SysParamEnum.IMAGE_PLAT_FORM_FLAG);
    //     if (null == sysParam || AppParamConstant.NO.equals(sysParam.getParamStatus()) || AppParamConstant.NO.equals(sysParam.getParamValue())) {
    //         rglog.error("本行未配置影像平台.");
    //         return false;
    //     }
    //     rglog.info("已配置影像平台,尝试从影像平台获取数据.");
    //
    //     MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
    //     if (null != mchntImageInfo) {
    //         if (!StringUtil.isEmptyOrNull(mchntImageInfo.getImageRemotePath())) {
    //             String parentPath = getImageParentPath(mchntImageInfo.getInstId());
    //             int index = mchntImageInfo.getImageLocalPath().indexOf(".");
    //             String fileFormt = mchntImageInfo.getImageLocalPath().substring(index);
    //             String localPath = parentPath + mchntImageInfo.getImageLocalPath();
    //
    //             ImagePlatfromBean imagePlatfromBean = new ImagePlatfromBean();
    //             imagePlatfromBean.setContentID(mchntImageInfo.getImageRemotePath());
    //             imagePlatfromBean.setFilePartName(localPath);
    //             imagePlatfromBean.setBusiSerialNo(RandomUtil.getUUID());
    //             List<ImagePlatformFileBean> imagePlatformFileBeanList = new ArrayList<>();
    //             imagePlatfromBean.setFileBeas(imagePlatformFileBeanList);
    //             ImagePlatformFileBean imagePlatformFileBean = new ImagePlatformFileBean();
    //             imagePlatformFileBeanList.add(imagePlatformFileBean);
    //             imagePlatformFileBean.setLocalFilePath(localPath);
    //             imagePlatformFileBean.setFileFormt(fileFormt);
    //
    //             rglog.info("影像平台下载:" + JSONObject.toJSONString(imagePlatfromBean));
    //             imagePlatfromBean = ImagePlatformUtil.queryImage(imagePlatfromBean, rglog);
    //             if (null != imagePlatfromBean) {
    //                 rglog.info("下载结果：" + JSONObject.toJSONString(imagePlatfromBean));
    //                 return true;
    //             }
    //         }
    //     }
    //     return false;
    // }
    //
    // private String getImageParentPath(String instId) throws Exception {
    //     SysParamService sysParamService = new SysParamServiceImpl();
    //     SysParam sysParam = sysParamService.getSysParamInfo(instId, BompAppParamConstant.IMAGE_STORAGE_PARAM_TYPE, BompAppParamConstant.LOCAL_IMAGE_STORAGE_PATH);
    //     if (null == sysParam) {
    //         rglog.error("查询系统参数表，根据图片类型获取图片存储服务器路径为空！ImageType：<{}>!");
    //         throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
    //     }
    //
    //     String parentPath;
    //     if (sysParam.getParamValue().trim().endsWith(File.separator)) {
    //         parentPath = sysParam.getParamValue().trim();
    //     } else {
    //         parentPath = sysParam.getParamValue().trim() + File.separator;
    //     }
    //     return parentPath;
    // }

}
